package com.yxq.mdt.app.modules.spikeorder.ui.adapter;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.adapter.UploadImageListAdapter;
import com.yxq.mdt.app.modules.bean.UploadImageBean;
import com.yxq.mdt.app.modules.picturebrowse.ui.PictureActivity;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderCommentPreviewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author L
 * @time 2019-11-08
 * @describe 提交商品评价列表$
 */
public class SpikeOrderCommentListAdapter extends BaseQuickAdapter<OrderCommentPreviewBean.GoodsListBean, BaseViewHolder> {

    List<UploadImageBean> uploadImageBeanList;
    OrderCommentInterface orderCommentInterface;

    public void setOrderCommentInterface(OrderCommentInterface orderCommentInterface) {
        this.orderCommentInterface = orderCommentInterface;
    }

    public interface OrderCommentInterface {
        void selectImage(int position, UploadImageListAdapter adapter);
    }

    public SpikeOrderCommentListAdapter(int layoutResId, @Nullable List<OrderCommentPreviewBean.GoodsListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderCommentPreviewBean.GoodsListBean item) {

        GlideHelper.setLoadRoundImage(mContext, item.getGoods_pic(), 0, helper.getView(R.id.comment_iv_img));

        RecyclerView recyclerView = helper.getView(R.id.recyclerView);
        //初始化图片上传控件
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        uploadImageBeanList = new ArrayList<>();
        uploadImageBeanList.add(new UploadImageBean());
        UploadImageListAdapter uploadImageListAdapter = new UploadImageListAdapter(uploadImageBeanList);
        recyclerView.setAdapter(uploadImageListAdapter);
        uploadImageListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (uploadImageListAdapter.getItem(position).getItemType() == -1) {
                    if (orderCommentInterface != null) {
                        orderCommentInterface.selectImage(helper.getAdapterPosition(), uploadImageListAdapter);
                    }
                }else{
                    //预览网络图片
                    if (uploadImageListAdapter.getData() != null && uploadImageListAdapter.getData().size() > 0) {
                        Intent intent = new Intent(mContext, PictureActivity.class);
                        intent.putExtra("position", position);
                        ArrayList<String> images = new ArrayList<>();
                        for (UploadImageBean uploadImageBean : uploadImageListAdapter.getData()) {
                            if (uploadImageBean.getItemType() != -1) {
                                images.add(uploadImageBean.getPath());
                            }
                        }
                        intent.putExtra("images", images);
                        mContext.startActivity(intent);
                    }
                }
            }
        });
        uploadImageListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<UploadImageBean> uploadImageBeanList = uploadImageListAdapter.getData();
                if (uploadImageBeanList.get(0).getItemType() != -1) {
                    uploadImageBeanList.remove(position);
                    //第一张不是添加图片，添加按钮
                    List<UploadImageBean> temp = new ArrayList<>();
                    temp.add(new UploadImageBean());
                    temp.addAll(uploadImageBeanList);
                    uploadImageListAdapter.setNewData(temp);
                } else {
                    uploadImageListAdapter.remove(position);
                }
            }
        });

        RadioGroup radioGroup = helper.getView(R.id.comment_rg);
        radioGroup.setOnCheckedChangeListener((radioGroup1, i) -> {
            switch (i) {
                case R.id.comment_rb_goods:
                    item.setScore(3);
                    break;
                case R.id.comment_rb_normal:
                    item.setScore(2);
                    break;
                case R.id.comment_rb_bad:
                    item.setScore(1);
                    break;
            }
        });
        EditText editText = helper.getView(R.id.comment_et_content);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                item.setContent(editText.getText().toString());
            }
        });
    }
}
